package cn.buk.qms.web;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.tms.dto.FlightChangeOrderDto;
import cn.buk.tms.entity.order.flight.FlightChangeOrderDetail;
import cn.buk.tms.entity.misc.FlightInfo;
import cn.buk.tms.entity.misc.OperationLog;
import cn.buk.tms.sc.SearchFlightOrderCriteria;
import cn.buk.qms.service.FlightChangeOrderService;
import java.util.List;
import jakarta.servlet.http.HttpServletRequest;

import cn.buk.tms.flight.dto.response.ChangeOrderResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 机票改签控制器
 * @author yfdai
 */
@RestController
@RequestMapping("/order/flight/change")
public class OrderChangeController extends BaseController {

    @Autowired
    private FlightChangeOrderService service;

    /**
     * 创建机票改签订单
     * 目前改签单处的再次改签用到了此方法
     */
    @PostMapping(value = "/create", consumes = "application/json")
    public JsonResult createFlightChangeOrder(HttpServletRequest request,
                                              @RequestBody FlightChangeOrderDto dto
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String operator = getUsername(request);

        return service.createFlightChangeOrder(enterpriseId, operator, dto);
    }

    /**
     * 创建机票改签订单
     */
    @PostMapping(value = "/createV2", consumes = "application/json")
    public JsonResult createFlightChangeOrderV2(HttpServletRequest request,
                                              @RequestBody FlightChangeOrderDto dto) {
        final int enterpriseId = getEnterpriseId(request);
        final String operator = getUsername(request);

        return service.createFlightChangeOrderV2(enterpriseId, operator, dto);
    }


    /**
     * 我来处理改签
     */
    @RequestMapping("/{id}/status/processed")
    public JsonResult processFlightChangeOrder(HttpServletRequest request,
                                          @PathVariable("id") int orderId) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.processFlightChangeOrder(enterpriseId, username, orderId);
    }

    /**
     * 改签完成，回填新票号和新编码
     */
    @PostMapping("/{id}/status/finished")
    public JsonResult finishFlightChangeOrder(HttpServletRequest request,
                                         @PathVariable("id") int orderId,
                                         @RequestParam("pnrNo") String pnrNo,
                                         @RequestParam("ticketNo") String ticketNo
                                              ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.finishFlightChangeOrder(enterpriseId, username, orderId, pnrNo, ticketNo);
    }

    /**
     * 取消改签订单
     */
    @PostMapping("/{id}/status/canceled")
    public JsonResult cancelFlightChangeOrder(HttpServletRequest request,
                                              @PathVariable("id") int orderId,
                                              @RequestParam("remark") String remark
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.cancelFlightChangeOrder(enterpriseId, username, orderId, remark);
    }

    /**
     * 重新打开已取消的改签订单
     */
    @RequestMapping("/{id}/status/reopened")
    public JsonResult reopenFlightChangeOrder(HttpServletRequest request,
                                              @PathVariable("id") int orderId
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.reopenFlightChangeOrder(enterpriseId, username, orderId);
    }

    /**
     * 查找改签订单
     */
    @GetMapping("/")
    public CommonDto<ChangeOrderResponseDto> searchFlightChangeOrders(HttpServletRequest request,
                                                                 @ModelAttribute("sc") SearchFlightOrderCriteria sc) {
        final int enterpriseId = getEnterpriseId(request);
        final int userId = getUserId(request);

        return service.searchFlightChangeOrder2(enterpriseId, userId, sc);
    }

    /**
     * 获取改签订单详情
     */
    @RequestMapping("/{id}")
    public ChangeOrderResponseDto getFlightChangeOrderDetail(HttpServletRequest request,
                                            @PathVariable("id") int id) {
        final int enterpriseId = getEnterpriseId(request);
        final int userId = getUserId(request);

        return service.getFlightChangeOrderDetail(enterpriseId, userId, id);
    }

    /**
     * 测试：发送改签单的模板信息
     */
    @RequestMapping("/{id}/weixinTplMsg")
    public JsonResult sendWeixinTplMsg(HttpServletRequest request,
                                       @PathVariable("id") int id)  {
        final int enterpriseId = getEnterpriseId(request);
        return service.sendWeixinTplMsg(enterpriseId, id);
    }

    /**
     * 获取改签订单操作记录
     */
    @RequestMapping("/{id}/history")
    public List<OperationLog> searchFlightChangeOrderHistory(HttpServletRequest request,
                                                             @PathVariable("id") int id
    ) {
        final int enterpriseId = getEnterpriseId(request);

        return service.searchFlightChangeOrderHistory(enterpriseId, id);
    }

    /**
     * 修改改签单的供应商
     */
    @PostMapping(value = "/{id}/supplier")
    public JsonResult updateChangeOrderSupplier(HttpServletRequest request,
                                             @PathVariable("id") int orderId,
                                             @RequestParam("supplierId") int supplierId
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.updateChangeOrderSupplier(enterpriseId, username, orderId, supplierId);
    }

    /**
     * 修改改签单的支付方式
     */
    @PostMapping(value = "/{id}/payment")
    public JsonResult updateChangeOrderPayment(HttpServletRequest request,
                                            @PathVariable("id") int orderId,
                                            @RequestParam("paymentMethodId") int paymentMethodId
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.updateChangeOrderPayment(enterpriseId, username, orderId, paymentMethodId);
    }

    /**
     * 修改改签单的备注
     */
    @PostMapping(value = "/{id}/remark")
    public JsonResult updateChangeOrderRemark(HttpServletRequest request,
                                               @PathVariable("id") int orderId,
                                               @RequestParam("remark") String remark
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.updateChangeOrderRemark(enterpriseId, username, orderId, remark);
    }

    /**
     * 修改改签单的票号
     */
    @PostMapping(value = "/{id}/ticket")
    public JsonResult updateChangeOrderTicket(HttpServletRequest request,
        @PathVariable("id") int orderId,
        @RequestParam("ticketNo") String ticketNo
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.updateChangeOrderTicketNo(enterpriseId, username, orderId, ticketNo);
    }

    /**
     * 修改改签单的价格
     */
    @PostMapping(value = "/{id}/price")
    public JsonResult updateChangeOrderPrice(HttpServletRequest request,
                                             @PathVariable("id") int id,
                                             @RequestParam("airChangeCharge") double airChangeCharge,
                                             @RequestParam("serviceCharge") double serviceCharge
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.updateChangeOrderPrice(enterpriseId, username, id, airChangeCharge, serviceCharge);
    }

    /**
     * 修改改签单的航班信息
     */
    @PostMapping(value = "/{id}/flight")
    public JsonResult updateChangeOrderFlight(HttpServletRequest request,
                                             @PathVariable("id") int id,
                                             @RequestParam("flightId") int flightId,
                                             @RequestParam("dport") String dport,
                                             @RequestParam("dtime") String dtime,
                                             @RequestParam("dterm") String dterm,
                                             @RequestParam("aport") String aport,
                                             @RequestParam("aterm") String aterm,
                                             @RequestParam("atime") String atime,
                                             @RequestParam("flightNo") String flightNo,
                                             @RequestParam("ddate") String ddate,
                                             @RequestParam("subclass") String subclass
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        FlightChangeOrderDetail detail = new FlightChangeOrderDetail();
        detail.setId(flightId);

        FlightInfo flt = detail.getFlight();
        flt.setDepartureAirport(dport);
        flt.setDepartureTime(dtime);
        flt.setDepartureTerminal(dterm);

        flt.setArrivalAirport(aport);
        flt.setArrivalTime(atime);
        flt.setArrivalTerminal(aterm);

        flt.setFlightNo(flightNo);
        flt.setSubclass(subclass);
        flt.setDepartureDate(ddate);

        return service.updateChangeOrderFlightInfo(enterpriseId, username, id, detail);
    }


    /**
     * CZ NDC 创建改签单
     */
    @PostMapping(value = "/{id}/externalOrderChange")
    public JsonResult createNdcOrderChange(HttpServletRequest request,
                                           @PathVariable("id") int orderId
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.createNdcOrderChange(enterpriseId, username, orderId);
    }

    /**
     * CZ NDC 查询改签单
     */
    @PostMapping(value = "/{id}/externalOrderChange/status")
    public JsonResult syncNdcOrderChangeInfo(HttpServletRequest request,
                                             @PathVariable("id") int orderId
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.syncNdcOrderChangeInfo(enterpriseId, username, orderId);
    }

    /**
     * CZ NDC 支付
     */
    @PostMapping(value = "/{orderId}/externalOrderChange/pay")
    public JsonResult payForFlightChangeOrder(HttpServletRequest request,
                                        @PathVariable("orderId") int orderId
    ) {
        final int enterpriseId = getEnterpriseId(request);
        final String username = getUsername(request);

        return service.payForFlightChangeOrder(enterpriseId, username, orderId);
    }
}